﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using EPiServer;
using EPiServer.PlugIn;
using EPiServer.Web;

namespace TemplateFoundation.MetaWeblog
{
    /// <summary>
    /// Page header used for Live Writer detection
    /// </summary>
    [PagePlugIn("EditUri", "Extra header for Windows Live Writer detection")]
    public class EditUriHeader
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="EditUriHeader"/> class.
        /// </summary>
        /// <param name="page">The page.</param>
        public EditUriHeader(PageBase page)
        {
            if (page != null)
            {
                page.PreRender += page_PrePrender;
            }
        }

        /// <summary>
        /// Initializes the specified option flag.
        /// </summary>
        /// <param name="optionFlag">The option flag.</param>
        public static void Initialize(int optionFlag)
        {
            PageBase.PageSetup += PageSetup;
        }

        private void page_PrePrender(object sender, EventArgs e)
        {
            Page page = sender as Page;
            if ((page != null) && (((PageBase)page).CurrentPageLink != null))
            {
                string pageId = ((PageBase)page).CurrentPageLink.ID.ToString();

                if (HttpRequestSupport.IsSystemDirectory(page.Request.Path))
                {
                    if (!page.Request.Path.ToLower().Contains("login.aspx"))
                    {
                        return;
                    }
                    if (string.IsNullOrEmpty(page.Request.QueryString["id"]))
                    {
                        return;
                    }
                    pageId = page.Request.QueryString["id"];
                }
                if (page.Header != null)
                {
                    HtmlLink editLink = new HtmlLink();
                    editLink.Attributes.Add("rel", "EditURI");
                    editLink.Attributes.Add("type", "application/rsd+xml");
                    editLink.Href = string.Concat(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + "/rsd.xml");
                    editLink.Attributes.Add("title", "RSD");
                    page.Header.Controls.Add(editLink);
                    page.Header.Controls.Add(new Literal() { Text = Environment.NewLine });

                    var wlwLink = new HtmlLink();
                    wlwLink.Attributes.Add("rel", "wlwmanifest");
                    wlwLink.Attributes.Add("type", "application/wlwmanifest+xml");
                    wlwLink.Href = string.Concat(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + "/wlwmanifest.xml");
                    wlwLink.Attributes.Add("title", "windows livewriter manifest");
                    page.Header.Controls.Add(wlwLink);
                    page.Header.Controls.Add(new Literal() { Text = Environment.NewLine });
                }
            }
        }

        private static void PageSetup(PageBase page, PageSetupEventArgs e)
        {
            new EditUriHeader(page);
        }
    }
}
